package com.standardoperationapp.operationplan;

import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.standardoperationapp.R;
import com.standardoperationapp.adapter.CheckNumListAdapter;
import com.standardoperationapp.bean.CheckNumBean;
import com.standardoperationapp.databinding.ActivityCheckNumBinding;
import com.standardoperationapp.global.GsonUtils;
import com.standardoperationapp.net.OnSuccessAndFaultListener;
import com.standardoperationapp.net.OnSuccessAndFaultSub;
import com.standardoperationapp.net.OperationApi;
import com.standardoperationapp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工器具核验页面
 */
public class CheckNumActivity extends AppCompatActivity {
    private int planId;
    private int workPointId;
    private int id;
    private ActivityCheckNumBinding binding;

    CheckNumListAdapter adapter;
    private List<CheckNumBean.DataBean> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_check_num);

        planId = getIntent().getIntExtra("planId", 0);
        workPointId = getIntent().getIntExtra("workPointId", 0);
        id = getIntent().getIntExtra("id", 0);
        initData();

        binding.btnSubmit.setOnClickListener(view -> submit());
    }

    private void initData() {

        OperationApi.getDetailTools(planId+"", new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                CheckNumBean bean = GsonUtils.fromJson(result, CheckNumBean.class);
                dataList = bean.getData();
                adapter = new CheckNumListAdapter(CheckNumActivity.this, dataList);
                binding.listView.setAdapter(adapter);
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }

    private void submit() {

        OperationApi.updateTools(dataList, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                ToastUtil.showToast("提交成功");
                submitExecuteStatus(id);

            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }

    /**
     * 执行状态确认
     */
    private void submitExecuteStatus(int pointExecutableMethodId) {
        Map<String, Object> map = new HashMap<>();
        map.put("planId", planId);
        map.put("workPointId", workPointId);
        map.put("pointExecutableMethodId", pointExecutableMethodId);
        map.put("executableStatus", 1);
        OperationApi.recordPliAndDetail(map, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                ToastUtil.showToast("提交成功");
                finish();
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }
}